var app = getApp();
var http_service = app.data.service;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpService:http_service,
    childService:[],
    img: [true,false,false,false],
    unlogin: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      //url传参数过来   父级项目id
      var serviceId = options.service_id;

      //页面加载 请求以选择的大项目的
      wx.request({
        url: http_service + '/index.php/getservicebypid?id=' + 7,
        success:function(res){
          //回调成功，将请求的返回结果赋值给data中的childService
          //console.log(res.data)
          that.setData({
            childService :res.data
          })
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  onGotUserInfo: function (e) {
    var that = this;
    //console.log("err:" + e.detail.errMsg)
    //console.log(e.detail.userInfo)
    if (e.detail.errMsg == "getUserInfo:fail auth deny")
      return;
    var userInfo = e.detail.userInfo
    //console.log(e.detail.rawData)

    wx.getSetting({
      success(res) {
        //没有授权
        if (!res.authSetting['scope.userInfo']) {
          that.setData({
            unlogin: true
          })
        } else {
          that.setData({
            unlogin: false
          })
        }
      }
    })

    wx.getStorage({
      key: 'openid',
      success: function (res) {
        wx.request({
          url: 'https://meile1990.meileholdings.com/miku/index.php/adduser',
          data: {
            openid: res.data,
            nick_name: userInfo.nickName
          },
          method: 'POST',
          success: function (res) {
            // console.log(res.data);
            wx.setStorage({
              key: 'userid',
              data: res.data,
            })
          }
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.data.hide = false;
    var that = this;
    //获取是否已经授权
    wx.getSetting({
      success(res) {
        //没有授权
        if (!res.authSetting['scope.userInfo']) {
          that.setData({
            unlogin: true
          })
        } else {
          that.setData({
            unlogin: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },


  //点击事件 这是点击项目预约
  click:function(e){
    //这是最终确认选择的service的id
    var serviceId = e.currentTarget.id;
    //将选择的服务id信息存储在本地
    wx.setStorage({
      key: 'serviceId',
      data: serviceId,
    })
    //跳转到确认页
    wx.navigateTo({
      url: '/pages/index/home?serviceId='+serviceId,
    })
  },

  //点击切换项目种类
  changeService:function(e){

    wx.showLoading({
      title: '加载中',
    })

    var serviceId = e.currentTarget.id;
    var that = this;
    //头部大类状态
    var img = [];
    for(var i = 0 ;i<4 ;i++){
      if(i==(serviceId-7)){
        img[i] = true;
      }else{
        img[i] = false;
      }
    }
    that.setData({
      img:img
    })
    wx.request({
      url: http_service + '/index.php/getservicebypid?id=' + serviceId,
      success: function (res) {
        wx.hideLoading()
        //回调成功，将请求的返回结果赋值给data中的childService
        //console.log(res.data)
        that.setData({
          childService: res.data
        })
      }
    })
  },
  detals: function (e) {
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: 'serviceinfo?serviceId=' + id,
    })
  }


})